package main;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class OpHDFSTools {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
		//	Map<String,String> cateInfo=new HashMap<String,String>();
		//	cateInfo=getFormatMapFormHDFSFile("345.txt","\t",5);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
	public static String getStringDate(Date date, String pattern) {
		try {
			SimpleDateFormat f = new SimpleDateFormat(pattern);
			String strdate = f.format(date);
			return strdate;
		} catch (Exception pe) {
		}
		return null;/**/
	}
	   public static Date getDateBefore(Date d, int day) {  
	       Calendar now = Calendar.getInstance();  
	       now.setTime(d);  
	       now.set(Calendar.DATE, now.get(Calendar.DATE) - day);  
	       return now.getTime();  
	   }  
	public static List<String> getStrListFormHDFSFile(String fpath) throws Exception
    {
        
		
        BufferedReader fileBR = makeBufferReader(fpath);
        
		List<String> lineList=new ArrayList<String>();
		
		//遍历文件
		String strLineBuff="";
		try
		{
			while((strLineBuff=fileBR.readLine()) != null)
			{
				strLineBuff = strLineBuff.trim();
//				System.out.println("strLineBuff:"+strLineBuff);
				if(strLineBuff!=null && !"".equals(strLineBuff))
				{
					lineList.add(strLineBuff);
				}
			}
		}
		catch(Exception e)
		{
			//e.printStackTrace();
			//System.out.println(fpath);
		}
		
		killBufferReader(fileBR);
		
//		System.out.println(fileBR.readLine());
		
		return lineList;
    }
	/**
	 * 获得搜索日志中某个数据
	 * */
	public static Map<String,String> getFormatMapFormHDFSFile(String fpath,String sep,int num) throws Exception
	{
        BufferedReader fileBR = makeBufferReader(fpath);
        
        Map<String,String> lineInfo=new HashMap<String,String>();
		
		//遍历文件
		String strLineBuff="";
		try
		{
			while((strLineBuff=fileBR.readLine()) != null)
			{
				if(strLineBuff!=null && !"".equals(strLineBuff))
				{
					try
					{
						String [] strArr=strLineBuff.split(sep,2);
						String valArr[]=strArr[1].split("\t",num);
						String tString="";
						for(int i=0;i<valArr.length;i++)
						{
							if("".equals(valArr[i])||valArr[i]==null||i>1)//控制输出级别
							{
								valArr[i]="all";
								break;
							}else {
								tString=tString+"\t"+valArr[i].trim();
								tString=tString.trim();
							}
						}
						strArr[1]=tString;
						lineInfo.put(strArr[0],strArr[1]);
					}
					catch(Exception e)
					{}
				}
			}
		}
		catch(Exception e)
		{
			/*e.printStackTrace();
			System.out.println(fpath);*/
		}
		
		killBufferReader(fileBR);
		return lineInfo;
	}
	
	public static Map<String,String> getMapFormHDFSFile(String fpath,String sep) throws Exception
    {
        BufferedReader fileBR = makeBufferReader(fpath);
        
        Map<String,String> lineInfo=new HashMap<String,String>();
		
		//遍历文件
		String strLineBuff="";
		try
		{
			while((strLineBuff=fileBR.readLine()) != null)
			{
				if(strLineBuff!=null && !"".equals(strLineBuff))
				{
					try
					{
						String [] strArr=strLineBuff.split(sep,2);
						lineInfo.put(strArr[0],strArr[1]);
					}
					catch(Exception e)
					{}
				}
			}
		}
		catch(Exception e)
		{
			/*e.printStackTrace();
			System.out.println(fpath);*/
		}
		
		killBufferReader(fileBR);
		return lineInfo;
    }
	
	public static Map<String,String> getMapFormHDFSFileForChanel(String fpath,String sep) throws Exception
    {
        BufferedReader fileBR = makeBufferReader(fpath);
        
        Map<String,String> lineInfo=new HashMap<String,String>();
		
		//遍历文件
		String strLineBuff="";
		try
		{
			while((strLineBuff=fileBR.readLine()) != null)
			{
				if(strLineBuff!=null && !"".equals(strLineBuff))
				{
					try
					{
						String [] strArr=strLineBuff.split(sep,2);
						if("fangchan".equals(strArr[1]))
						{
							strArr[1]="房产";
						}else if("ershou".equals(strArr[1]))
						{
							strArr[1]="二手";
						}else if("huangye".equals(strArr[1]))
						{
							strArr[1]="黄页";
							
						}else if("zhaopin".equals(strArr[1]))
						{
							strArr[1]="招聘";
							
						}else if("ershouche".equals(strArr[1]))
						{
							strArr[1]="二手车";
						}else {
							strArr[1]="其他";
						}
						lineInfo.put(strArr[0],strArr[1]);
					}
					catch(Exception e)
					{}
				}
			}
		}
		catch(Exception e)
		{
			/*e.printStackTrace();
			System.out.println(fpath);*/
		}
		
		killBufferReader(fileBR);
		return lineInfo;
    }
	
	
	public static void killBufferReader(BufferedReader reader)
	{
		try 
		{
			if(reader!=null)
			{
				reader.close();
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
	
	
	public static BufferedReader makeBufferReader(String ifpath) throws Exception 
	{
		//打开读文件
		String charset="utf-8";
		return makeBufferReader(ifpath,charset);
	}
	
	public static BufferedReader makeBufferReader(String ifpath,String charset) throws Exception 
	{
		//打开读文件
		Configuration config = new Configuration();
        FileSystem hdfs = FileSystem.get(config);
        Path path = new Path(ifpath);
        InputStreamReader isr = null;
		BufferedReader fileBR = null;
		
		try
		{
			isr = new InputStreamReader( hdfs.open(path),charset);
			fileBR = new BufferedReader(isr);
			return fileBR;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			//System.out.println("open file error!");
			if(fileBR!=null)
			{
				try
				{
					fileBR.close();
				}
				catch(IOException e1)
				{
					//e1.printStackTrace();
				}
			}
			if(isr!=null)
			{
				try
				{
					isr.close();
				}
				catch(IOException e1)
				{
					//e1.printStackTrace();
				}
			}
			return null;
		}
		
	}
	
	
	//创建目录
	public static void mkdir(String path) throws IOException
	{
	    Configuration conf = new Configuration();
	    FileSystem fs = FileSystem.get(conf);
	    Path srcPath = new Path(path);
	     fs.mkdirs(srcPath);
	   /* if(isok){   boolean isok =
	        System.out.println("create dir ok!");
	    }else{
	        System.out.println("create dir failure");
	    }*/
	    fs.close();
	}
	
	//创建新文件
	public static void createFile(String dst , byte[] contents) throws IOException
	{
	    Configuration conf = new Configuration();
	    FileSystem fs = FileSystem.get(conf);
	    Path dstPath = new Path(dst); //目标路径
	    //打开一个输出流
	    FSDataOutputStream outputStream = fs.create(dstPath);
	    outputStream.write(contents);
	    outputStream.close();
	    fs.close();
	    //System.out.println("文件创建成功！");
	}
	
}
